<% 
  String service = request.getParameter("service"); 
  if ( service == null || service.length() == 0 ) service = "geocoder/geocoder-service.xml"; 
  String doc = request.getParameter("document"); 
  if ( doc == null || doc.length() == 0 ) doc = ""; 
  String serviceURL = null;
  if ( service.equals("geocoder/geocoder-service.xml")) {
	serviceURL = "geocoder/geocoder-service.xml?service=geocode-full&doc=" + java.net.URLEncoder.encode(doc);
  } else if ( service.equals("geocoder-pt/geocoder-service.xml")) {
	serviceURL = "geocoder-pt/geocoder-service.xml?service=geocode-full&doc=" + java.net.URLEncoder.encode(doc);
  }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
   <title>Document Geocoding Web Service :: Live Demo</title>
   <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA7_kD1t_m22HBF9feCaDPZxQwcATY4FXmxYwkk9LNWGtAQdNKTBS1kBsTEqrRPg2kWxuNdmf2JVCIkQ" type="text/javascript"></script>
   <script>
     var map;
     function initialize () {  
      if (GBrowserIsCompatible()) {
         map = new GMap2(document.getElementById("map_canvas"));
         map.setCenter(new GLatLng(0.0, 0.0), 3);  
	 map.addControl(new GLargeMapControl());
         map.addControl(new GMapTypeControl());
         map.addMapType(G_PHYSICAL_MAP);
	 map.setMapType(G_PHYSICAL_MAP);
	 addMarkersFromXML();	 
      } 
     }
      
     function addMarkersFromXML(){
	   var batch = [];
	   var request = GXmlHttp.create();
	   request.open('GET', '<%= serviceURL %>', true);
	   request.onreadystatechange = function() {
  	   if (request.readyState == 4 && request.status == 200) {
		var xmlDoc = request.responseXML;
		var xmlrows = xmlDoc.documentElement.getElementsByTagName("geocode");
		for (var i = 0; i < xmlrows.length; i++) {
			var xmlrow = xmlrows[i];
			var xmlcellLongitude = xmlrow.getElementsByTagName("longitude")[0];
			var xmlcellLatitude = xmlrow.getElementsByTagName("latitude")[0];
			if(typeof xmlcellLatitude == 'undefined') continue;		
			if(typeof xmlcellLongitude == 'undefined') continue;
			var point = new GLatLng(parseFloat(xmlcellLatitude.firstChild.data), parseFloat(xmlcellLongitude.firstChild.data));
			var xmlcellHTM = xmlrow.getElementsByTagName("htm-code")[0];
			var celltextHTM = xmlcellHTM.firstChild.data;
			var xmlcellProb = xmlrow.getElementsByTagName("probability")[0];
			var celltextProb = xmlcellProb.firstChild.data;
			var htmlString = "<b>HTM Code:</b> " + celltextHTM + "<br/>" + 
                                         "<b>Probability:</b> " + celltextProb + "<br/>" + 
                                         "<b>Latitude:</b> " + xmlcellLatitude.firstChild.data + "<br/>" + 
                                         "<b>Longitude:</b> " + xmlcellLongitude.firstChild.data;	
			batch.push(marker);
			var marker = createMarker(point,htmlString);
		}
  	  }
        }
	request.send(null);	
     }
     
     function createMarker(point,html) {
	   var marker = new GMarker(point);
           GEvent.addListener(marker, "click", function() { map.openInfoWindowHtml(point, html); });
	   map.addOverlay(marker, { title: html });
           return marker;
     }
    </script>
  </head>
  <body onload="initialize()" onunload="GUnload()">
    <div id="map_canvas" style="width: 1350px; height: 800px"></div>
    <div id="message"></div>
  </body>
</html>
